home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!netnews
- From: miker3@ix.netcom.com (Mike Rubenstein)
- Newsgroups: comp.lang.c
- Subject: Re: Confusion as to the proper use of MODULAS
- Date: Wed, 14 Feb 1996 23:59:46 GMT
- Organization: Netcom
- Message-ID: <312266e3.78899070@nntp.ix.netcom.com>
- References: <4fr8be$ass@news.iconn.net> <31224679.6193@born.com> <4fthsu$1kl@ixnews7.ix.netcom.com>
- NNTP-Posting-Host: ix-dc10-04.ix.netcom.com
- X-NETCOM-Date: Wed Feb 14 3:59:27 PM PST 1996
- X-Newsreader: Forte Agent .99d/32.182
-
- wzjn@ix.netcom.com (KPN ) wrote:
-
- > Confusion as to the proper use of MODULAS
- >
- > Very confused. Just as I think IÆve got it correct, I make-up a new
- > test, the results of which leave me mixed-up again.
- >
- > To begin with, this is the first rule I found out:
- >
- > Modulas operator simply shows: *** AFTER *** you divide, what is the
- > remainder?
- > EXAMPLE of this:
- > 9 % 7 = ?
- > 9 divided by 7 yields a remainder of 2
- > 9 % 7 = 2
- >
- > 1) MODULAS means divide a number and give me the left over number. OK -
- > not bad.
- >
- > Next rule:
- > If the number to be divided is smaller than the number to divide by,
- > the result will always be the number being divided
- > EXAMPLE of this:
- >
- > 9 % 12 = ?
- > can not divide 9 by a larger number
- > 9 % 12 = 9
- > Now I have rule #2
- >
- > 2) If the number to be divided is smaller than the nuber to be used as
- > a divisor, the result is the original number. OK - again, not bad.
- >
- > Here, begin my troubles: using MODULAS in an IF statement.
- >
- > I made up a test to see if a number I inputted was a 7. Sample code:
- >
- > if (number % 7)
- > printf("Not a 7\n");
- > else
- > printf("First integer was a 7\n");
- >
- > So, if the number WAS a 7, the ELSE would take over. OK. But, when I
- > enter in a ZERO, it still tells me that the number was a seven?
- >
- > Am I going in the right direction? Can someone tell me what IÆm doing
- > incorrectly, or where IÆm straying? WhatÆs the rule here for using MOD
- > in an IF statement?
-
- The problem is that you're printing the wrong message. If number is
- nonnegative, number % 7 is the remainder when dividing number by 7.
- The remainder when dividing 0 by 7 is 0.
-
- In any case, if a divides b then b % a is 0. Your code should print
- that the integer was a 7 if it is ..., -14, -7, 0, 7, 14, ...
-
-
- Michael M Rubenstein
-